package org.budo.dubbo.protocol.http.page;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.budo.dubbo.protocol.http.page.util.InterfaceStructureUtil;
import org.budo.support.spring.io.util.ResourceUtil;

import com.alibaba.dubbo.common.utils.CollectionUtils;

/**
 * @author lmw
 */
public class IndexPage extends AbstractPage {
    private static final long serialVersionUID = -5900698757514185755L;

    protected void service(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        this.renderIndexPage(req, response);
    }

    private void renderIndexPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Scope scope = this.getScope(request);
        Map<String, Object> dataMap = CollectionUtils.toMap("interfaces", InterfaceStructureUtil.listInterfaceMap(scope));

        String templateInput = ResourceUtil.classPathResourceInputStreamToString("api-doc-template/index.htm");
        this.renderPage(templateInput, dataMap, request, response);
    }
}